/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2012 All Rights Reserved.
 */
package com.example.demo.swj.uitl;

import java.util.*;

public class NeuqSignature {


    public static String getSignatureContent(RequestParametersHolder requestHolder) {
        return getSignContent(getSortedMap(requestHolder));
    }

    public static Map<String, String> getSortedMap(RequestParametersHolder requestHolder) {
        Map<String, String> sortedParams = new TreeMap<String, String>();
        NeuqHashMap queryParams = requestHolder.getQueryParams();
        if (queryParams != null && queryParams.size() > 0) {
            sortedParams.putAll(queryParams);
        }
        NeuqHashMap body = requestHolder.getBody();
        if (body != null && body.size() > 0) {
            sortedParams.putAll(body);
        }
        return sortedParams;
    }

    /**
     *
     * @param sortedParams
     * @return
     */
    public static String getSignContent(Map<String, String> sortedParams) {
        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList<String>(sortedParams.keySet());
        Collections.sort(keys);
        int index = 0;
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = sortedParams.get(key);
            if (StringUtils.areNotEmpty(key, value)) {
                content.append((index == 0 ? "" : "&") + key + "=" + value);
                index++;
            }
        }
        return content.toString();
    }

    /**
     *  签名
     *
     * @param content
     * @param privateKey
     * @param charset
     * @return
     * @throws NeuqApiException
     */
    public static String sign(String appId, String content, String privateKey, String charset,
                              String signType) throws NeuqApiException {
        if (NeuqConstants.SIGN_TYPE_RSA2.equals(signType)) {
            return SignUtil.rsa2Sign(content, privateKey, charset);
        } else if (NeuqConstants.SIGN_TYPE_SM2.equals(signType)) {
            return SignUtil.smSign(appId, content, privateKey, charset);
        } else {
            throw new NeuqApiException("Sign Type is Not Support : signType=" + signType);
        }
    }

    public static String getSignCheckContent(Map<String, String> params) {
        if (params == null) {
            return null;
        }

        params.remove("sign");

        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            content.append((i == 0 ? "" : "&") + key + "=" + value);
        }

        return content.toString();
    }

    public static boolean signCheck(String appId, String content, String sign, String publicKey, String charset,
                                    String signType) throws NeuqApiException {
        if (NeuqConstants.SIGN_TYPE_RSA2.equals(signType)) {
            return SignUtil.rsa2SignValid(content, sign, publicKey, charset);
        } else if (NeuqConstants.SIGN_TYPE_SM2.equals(signType)) {
            return SignUtil.smSignValid(appId, content, sign,  publicKey, charset);
        } else {
            throw new NeuqApiException("Sign Type is Not Support : signType=" + signType);
        }
    }

}
